package com.google.code.swingonfire.border;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class ShadowBorderCacheEntry
{

    private final Image topLeftImage;
    private final Image topImage;
    private final Image topRightImage;
    private final Image leftImage;
    private final Image rightImage;
    private final Image bottomLeftImage;
    private final Image bottomImage;
    private final Image bottomRightImage;

    public ShadowBorderCacheEntry(ShadowBorderCacheKey key)
    {
        super();

        int size = key.getSize() * 2;

        topLeftImage = createImage(size, size, size - 1, size - 1, size, key.getInnerColor(), key.getOuterColor());
        topImage = createImage(1, size, 0, size - 1, size, key.getInnerColor(), key.getOuterColor());
        topRightImage = createImage(size, size, 0, size - 1, size, key.getInnerColor(), key.getOuterColor());
        leftImage = createImage(size, 1, size - 1, 0, size, key.getInnerColor(), key.getOuterColor());
        rightImage = createImage(size, 1, 0, 0, size, key.getInnerColor(), key.getOuterColor());
        bottomLeftImage = createImage(size, size, size - 1, 0, size, key.getInnerColor(), key.getOuterColor());
        bottomImage = createImage(1, size, 0, 0, size, key.getInnerColor(), key.getOuterColor());
        bottomRightImage = createImage(size, size, 0, 0, size, key.getInnerColor(), key.getOuterColor());
    }

    public Image getTopLeftImage()
    {
        return topLeftImage;
    }

    public Image getTopImage()
    {
        return topImage;
    }

    public Image getTopRightImage()
    {
        return topRightImage;
    }

    public Image getLeftImage()
    {
        return leftImage;
    }

    public Image getRightImage()
    {
        return rightImage;
    }

    public Image getBottomLeftImage()
    {
        return bottomLeftImage;
    }

    public Image getBottomImage()
    {
        return bottomImage;
    }

    public Image getBottomRightImage()
    {
        return bottomRightImage;
    }

    private static Image createImage(int width, int height, int centerX, int centerY, int size, Color innerColor,
        Color outerColor)
    {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        int da = outerColor.getAlpha() - innerColor.getAlpha();
        int dr = outerColor.getRed() - innerColor.getRed();
        int dg = outerColor.getGreen() - innerColor.getGreen();
        int db = outerColor.getBlue() - innerColor.getBlue();

        for (int y = 0; y < height; y += 1)
        {
            for (int x = 0; x < width; x += 1)
            {
                int dx = centerX - x;
                int dy = centerY - y;
                double d = (1 - Math.max(0, Math.min(1, (Math.sqrt((dx * dx) + (dy * dy)) / size))));
                d = 1 - (d * d);
                //                double d =
                //                    (1 - Math.cos(Math.max(0, Math.min(1, (Math.sqrt((dx * dx) + (dy * dy)) / size))) * Math.PI)) * 0.5;

                image.setRGB(
                    x,
                    y,
                    ((int) Math.round(innerColor.getAlpha() + (da * d)) << 24)
                        | ((int) Math.round(innerColor.getRed() + (dr * d)) << 16)
                        | ((int) Math.round(innerColor.getGreen() + (dg * d)) << 8)
                        | ((int) Math.round(innerColor.getBlue() + (db * d))));
            }
        }

        return image;
    }
}
